import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
const WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    news: {},
    uploadUrl: wx.getStorageSync('uploadUrl'),
    id:''
  }, 
    
  onLoad: function (options) {
    // console.log(options.id,'options.id')
    // this.setData({
    //   id: options.id
    // },()=>{
    //   this.getDetail()
    // }) 
    var pramas = options
    var obj = wx.getLaunchOptionsSync()
    //通过分享进来
    if (options.scene) {
      this.decodeShort(options.scene)
    } else {
      this.repramas(pramas)
    }  
  },


  //解析参数
  repramas: function (pramas) {
    this.setData({
      id: pramas.id,
      sharedCusId: pramas.sharedCusId || '',
      empUserId: pramas.empUserId || ''
    })

    if (pramas.sharedCusId) {
      wx.removeStorageSync('empUserId')
      wx.setStorageSync('sharedCusId', pramas.sharedCusId)
    }

    if (pramas.empUserId) {
      wx.removeStorageSync('sharedCusId')
      wx.setStorageSync('empUserId', pramas.empUserId)
    } 
    if (pramas.id) { 
      this.getDetail();  
    }
  },


  //将短码解码成长码
  decodeShort(scene) {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/comm/decodeShort',
      method: 'POST',
      data: {
        shortCode: scene
      } 
    }, (res) => {
      var longCode = res.longCode
      var pramas = _wxRequest.urlScene(decodeURIComponent(longCode))
      this.repramas(pramas) 
    })
  },


  //获取商品详情
  getDetail:function(){  
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/info/getById?fromMall=true&id=' + this.data.id,
      method: 'POST'
    }, (res) => {  
      this.setData({
        news:res,
        startDateText: res.startDate ? res.startDate.substring(0, 16) : '',
        expireDateText: res.expireDate ? res.expireDate.substring(0, 16) : '',
      })
      WxParse.wxParse('content', 'html', this.data.news.content, this, 25);
    })
  },

  toSto: function (e) {
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx.navigateTo({
      url: '/pages/store/detail?id=' + id,
    })
  },





})